package com.lanou.tipsy.adapter.songList;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.songList.songslist.SongListData;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/22.
 *
 *   V Z V
 *   歌单页面 GridView 适配器
 */
public class SongsListsAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<SongListData> data;
    private ImageLoader imageLoader;
    private VolleySingleton volleySingleton;


    public SongsListsAdapter(Context context, ArrayList<SongListData> data){
        this.data = data;
        this.context = context;
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton.getImageLoader();
    }

    public void refreshData(ArrayList<SongListData> data){
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHandler handler = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.fresco_songs_lists,null);
            handler = new ViewHandler();
            handler.frescoText = (TextView) convertView.findViewById(R.id.frescoText);
            handler.frescoImage = (SimpleDraweeView) convertView.findViewById(R.id.frescoImage);
            convertView.setTag(handler);
        }else {
            handler = (ViewHandler) convertView.getTag();
        }
        final SongListData data = (SongListData) getItem(position);
        if (data != null){
            handler.frescoText.setText("  "+data.getTitle());
            Uri uri = Uri.parse(data.getPic_url());
            handler.frescoImage.setImageURI(uri);
        }
       /* handler.frescoImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Sysout", "点击事件");

            }
        });*/
        return convertView;
    }
    private class ViewHandler{
        public TextView frescoText;
        public SimpleDraweeView frescoImage;
    }
}
